/*
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2004-2007 Intel Corporation. All Rights Reserved.
//
//
*/
#include "umc_defs.h"
#if defined (UMC_ENABLE_DV50_VIDEO_DECODER)

#include <ippi.h>
#include "umc_dv50_decoder.h"
#include "umc_dv50_internal.h"

namespace UMC
{

void store_dv50_segment_4s_reference(STORE_DV_SEGMENT_INFO &StoreInfo)
{
#undef WRITE_2PIXELS
#define WRITE_2PIXELS(pointer_dst, pointer_y, pointer_u, pointer_v) \
    (pointer_dst)[0] = SaturateByte((*(pointer_y + 0) + *(pointer_y + 1) + *(pointer_y + 2) + *(pointer_y + 3) + \
                                     *(pointer_y + 8) + *(pointer_y + 9) + *(pointer_y + 10) + *(pointer_y + 11) +   \
                                     *(pointer_y + 16) + *(pointer_y + 17) + *(pointer_y + 18) + *(pointer_y + 19) + \
                                     *(pointer_y + 24) + *(pointer_y + 25) + *(pointer_y + 26) + *(pointer_y + 27)) / 16 + 128); \
    (pointer_dst)[1] = SaturateByte((*(pointer_u + 0) + *(pointer_u + 1) + *(pointer_u + 2) + *(pointer_u + 3) + \
                                     *(pointer_u + 8) + *(pointer_u + 9) + *(pointer_u + 10) + *(pointer_u + 11) +   \
                                     *(pointer_u + 16) + *(pointer_u + 17) + *(pointer_u + 18) + *(pointer_u + 19) + \
                                     *(pointer_u + 24) + *(pointer_u + 25) + *(pointer_u + 26) + *(pointer_u + 27)) / 16 + 128); \
    (pointer_dst)[2] = SaturateByte((*(pointer_y + 4) + *(pointer_y + 5) + *(pointer_y + 6) + *(pointer_y + 7) + \
                                     *(pointer_y + 12) + *(pointer_y + 13) + *(pointer_y + 14) + *(pointer_y + 15) + \
                                     *(pointer_y + 20) + *(pointer_y + 21) + *(pointer_y + 22) + *(pointer_y + 23) + \
                                     *(pointer_y + 28) + *(pointer_y + 29) + *(pointer_y + 30) + *(pointer_y + 31)) / 16 + 128); \
    (pointer_dst)[3] = SaturateByte((*(pointer_v + 0) + *(pointer_v + 1) + *(pointer_v + 2) + *(pointer_v + 3) + \
                                     *(pointer_v + 8) + *(pointer_v + 9) + *(pointer_v + 10) + *(pointer_v + 11) +   \
                                     *(pointer_v + 16) + *(pointer_v + 17) + *(pointer_v + 18) + *(pointer_v + 19) + \
                                     *(pointer_v + 24) + *(pointer_v + 25) + *(pointer_v + 26) + *(pointer_v + 27)) / 16 + 128);

    Ipp16s *lpsSrc, *lpsSrcV;
    Ipp8u *lpbDst;
    Ipp32u i, MacroBlock;

    for (MacroBlock = 0;MacroBlock < 5;MacroBlock += 1)
    {
        lpsSrc = (Ipp16s *) StoreInfo.m_lpsSource[MacroBlock];
        lpsSrcV = lpsSrc + 4 * 64;
        lpbDst = StoreInfo.m_lpbDestination[MacroBlock];

        // write macro block
        for (i = 0;i < 2;i += 1)
        {
            // block y0
            WRITE_2PIXELS(lpbDst + 0, lpsSrc + 0, lpsSrcV + 64, lpsSrcV + 0)

            // block y1
            WRITE_2PIXELS(lpbDst + 4, lpsSrc + 128, lpsSrcV + 68, lpsSrcV + 4)

            lpbDst += StoreInfo.m_lPitch;
            lpsSrc += 8 * 4;
            lpsSrcV += 8 * 4;
        }
    }

} // void store_dv50_segment_4s_reference(STORE_DV_SEGMENT_INFO &StoreInfo)

void StoreDV50Segment_YUY2_4s(STORE_DV_SEGMENT_INFO &StoreInfo)
{
    // place you optimized code here
    // ...

    store_dv50_segment_4s_reference(StoreInfo);

} // void StoreDV50Segment_YUY2_4s(STORE_DV_SEGMENT_INFO &StoreInfo)

void DV50VideoDecoder::StoreDV50Segment_4s(Ipp32u i, Ipp32u k, Ipp32u nThreadNum)
{
    STORE_DV_SEGMENT_INFO RealStoreInfo;

    // reset pointers
    InitializeStoreDV50Info(RealStoreInfo, i, k, nThreadNum);

    StoreDV50Segment_YUY2_4s(RealStoreInfo);

} // void DV50VideoDecoder::StoreDVSegment_4s(Ipp32u i, Ipp32u k, Ipp32u nThreadNum)

} // end namespace UMC

#endif //(UMC_ENABLE_DV50_VIDEO_DECODER)
